import random
import re
import time

import requests
from bs4 import BeautifulSoup


class album_info(object):
    def set_user_agent(self):
        USER_AGENTS = [
            "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
            "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
            "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
            "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
            "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
            "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5"
        ]
        user_agent = random.choice(USER_AGENTS)
        return user_agent

    def __init__(self):
        self.song_url = 'https://music.163.com/song'
        self.album_url = 'https://music.163.com/album'

    def spider(self, url, id):
        time.sleep(0.5)
        url =url
        params = {'id':id}
        headers = {'User-Agent': self.set_user_agent()}
        response = requests.get(url=url, params=params, headers=headers).content.decode()
        soup = BeautifulSoup(response, 'lxml')

        return soup


    def get_album_id(self, song_id):
        soup = self.spider(url=self.song_url, id=song_id)
        content = soup.find_all(attrs={"property":"music:album"})
        #print(content)
        #<meta property="music:album" content="https://music.163.com/album?id=96023669"/> property="music:album"/>]
        album_id = re.findall(r'id=(.*?)" property', str(content))[0]
        return album_id


    def get_album_info(self, album_id):
        soup = self.spider(url=self.album_url, id=album_id)
        scrpit = soup.find_all(attrs={"class":"intr"})
        album_info_dict = dict()
        #print(scrpit)
        try:
            singer = re.findall(r'title="(.*?)">', str(scrpit))[0]
            singer_info = f"作家：{singer}"
        except:
            singer_info = "无作家"
        finally:
            print(singer_info)
            album_info_dict['singer'] = singer_info

        try:
            publish_time = re.findall(r'<p class="intr"><b>发行时间：</b>(.*?)</p>', str(scrpit))[0]
            publish_time_info = f"发行时间：{publish_time}"
        except:
            publish_time_info = "无发行时间"
        finally:
            print(publish_time_info)
            album_info_dict['publish_time'] = publish_time_info

        try:
            publisher = re.findall(r'发行公司：</b>\n(.*?)\n</p>', str(scrpit), re.S)[0]
            publisher_info = f"发行厂牌：{publisher}"
        except:
            publisher_info = "无发行厂牌"
        finally:
            print(publisher_info)
            album_info_dict['publisher'] = publisher_info

        return album_info_dict


    def run_album(self, song_id):
        print('获取专辑信息——————————————————')
        album_id = self.get_album_id(song_id)
        album_info_dict = self.get_album_info(album_id)
        
        print('获取专辑信息部分完成！——————————————————')
        return album_info_dict


    def run_album_inside(self):
        '''
        内部使用，判断id
        :return:
        '''
        print('获取专辑信息——————————————————')
        flag = int(input('1：歌曲id， 2：专辑id\n'))
        if flag == 1:
            song_id = input('网易云歌曲id：')  #   1482614803
            album_id = self.get_album_id(song_id)
            self.get_album_info(album_id)
        elif flag == 2:
            album_id = input('网易云专辑id：') #  96023669
            album = self.get_album_info(album_id)
        else:
            print('输错了！')
            self.run_album_inside()
        print('获取专辑信息完成！——————————————————')

if __name__ == '__main__':
    info = album_info()
    info.run_album_inside()



